package kr.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import voSetting.EmployeeSearchVO;

public class EmployeeSearchDTO {
	private Connection conn = null;
	private EmployeeSearchVO vo = null;
	
	public EmployeeSearchDTO(){
		conn = new MySql().getConn();
	}
	
	public EmployeeSearchDTO(EmployeeSearchVO v){
		this.vo = v;
		conn = new MySql().getConn();
	}
	
	
	public ArrayList<EmployeeSearchVO> select(EmployeeSearchVO esvo){
		this.vo = esvo;
		ArrayList<EmployeeSearchVO> al = new ArrayList<EmployeeSearchVO>();
		String sql = "select e.e_name, e.e_position, d1.d_tname , d2.d_tname, m.m_id "
				+ "	from semyadmin.employees e"
				+ "	inner join semyadmin.departments d1 on d1.d_code = e.d_code"
				+ "	inner join semyadmin.departments d2 on d1.d_pcode = d2.d_code " 
				+ " inner join semyadmin.messenger m on m.e_number = e.e_number " 
				+ " where m.m_id like ? or e.e_name like ? or d1.d_tname like ? or d2.d_tname like ?";

		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, "%" + vo.getFind() + "%");
			pst.setString(2, "%" + vo.getFind() + "%");
			pst.setString(3, "%" + vo.getFind() + "%");
			pst.setString(4, "%" + vo.getFind() + "%");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				System.out.println("들어옴?");
				EmployeeSearchVO v = new EmployeeSearchVO();
				v.setGroup(rs.getString("d2.d_tname"));
				v.setTeam(rs.getString("d1.d_tname"));
				v.setEmployee_name(rs.getString("e.e_name"));
				v.setEmployee_position(rs.getString("e.e_position"));	
				v.setM_id(rs.getString("m.m_id"));
				al.add(v);
			}
			rs.close();
			pst.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return al;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public EmployeeSearchVO getVo() {
		return vo;
	}

	public void setVo(EmployeeSearchVO vo) {
		this.vo = vo;
	}
	
}
